package com.mybatis02.mapper;

import com.mybatis02.pojo.Emp;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

/**
 * @author wzm
 * @create 2023-12-04 11:19
 * project_name:web2310
 * @Description
 */
@Mapper
public interface EmpMapper {

    //根据id删除员工
    @Delete("delete from emp where id = #{id}") //#{参数key}
    void delete(Integer id);

    //拿到新增生成自增长主键值
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("INSERT INTO emp (username,name, gender, image, job, entrydate, dept_id, create_time, update_time) VALUES(#{username},#{name},#{gender},#{image},#{job},#{entryDate},#{deptId},#{createTime},#{updateTime})")
    void insert(Emp emp);

    //根据id查询对象信息
    //当数据库字段与实体属性不一致时 方式一：起别名
    /*@Select("select id,username,password,name,gender,image,job,entrydate entryDate,dept_id deptId,create_time createTime,update_time updateTime from emp where id = #{id}")
    Emp selectById(Integer id);*/

    //根据id查询对象信息
    //当数据库字段与实体属性不一致时 方式一：手动映射查询的结果与属性
  /*  @Results({
            *//*手动将数据库的字段匹配属性*//*
            @Result(column = "dept_id",property = "deptId"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    @Select("select id,username,password,name,gender,image,job,entrydate,dept_id,create_time,update_time from emp where id = #{id}")
    Emp selectById(Integer id);*/

    //根据id查询对象信息
    //当数据库字段与实体属性不一致时 方式3：在appliction.properties中设置开启驼峰命名规则
    @Select("select id,username,password,name,gender,image,job,entrydate,dept_id,create_time,update_time from emp where id = #{id}")
    Emp selectById(Integer id);


    //根据id修改员工的信息
    @Update("update emp set username = #{username},password = #{password},name = #{name},gender = #{gender},image = #{image},job = #{job},entrydate = #{entryDate},dept_id = #{deptId},update_time = #{updateTime} where id = #{id}")
    void update(Emp emp);


    //条件查询 name 模糊查询  gender精确匹配 日期范围 方式一 使用${}拼接 有注入风险
   /* @Select("select * from emp where name like '%${name}%' and gender = #{gender} and entrydate between #{begin} and #{end} order by update_time desc")
    List<Emp> list(String name, Short gender, LocalDate begin,LocalDate end);
*/
    //条件查询 name 模糊查询  gender精确匹配 日期范围 方式二使用拼接concat(字符串1,字符串2,....)
    @Select("select * from emp where name like concat('%',#{name},'%') and gender = #{sex} and entrydate between #{begin} and #{end} order by update_time desc")
    List<Emp> list(String name,@Param("sex") Short gender, LocalDate begin,LocalDate end);
    //SpringBoot2.x只需要保证形参与#{key}中的key一致就可以获取值
    //早期springBoot1.x需要手动指定@Param("key")的key才能获取  当然2.x版本中可以用来匹配不同的形参名





}
